package com.hangwei.paperless.view.repair.accept;

import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.hangwei.paperless.MainRepairActivity;
import com.hangwei.paperless.R;
import com.hangwei.paperless.adapter.accept.AcceptStatusAdapter;
import com.hangwei.paperless.adapter.accept.Item1Adapter;
import com.hangwei.paperless.model.bean.AcceptCheckInfo;
import com.hangwei.paperless.model.bean.DictBean;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.hangwei.paperless.utils.Density;
import com.hangwei.paperless.utils.JsonPostUtil;
import com.hangwei.paperless.view.BaseView;
import com.hangwei.paperless.view.repair.AcceptCheck;
import com.hangwei.paperless.widget.MaxRecyclerView;
import com.hangwei.paperless.widget.MyOnItemSwipeListener;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.ToastUtil;
import com.twelve.all.utils.Tools;

import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class AcceptItem1 extends BaseView<MainRepairActivity, List<AcceptCheckInfo.TBean>> {

    private MaxRecyclerView mRecyclerView;
    private Item1Adapter mAdapter;

    public AcceptItem1(MainRepairActivity activity) {
        super(activity);
    }

    @Override
    protected View initView() {
        View inflate1 = LayoutInflater.from(mActivity).inflate(R.layout.page_accept_check_view1, null);
        TextView tvMsg1 = inflate1.findViewById(R.id.tvMsg);
        tvMsg1.setText("2、所接收到的LRU件清单 LRUs Received");
        TextView tvSave0 = inflate1.findViewById(R.id.tvSave);
        TextView tvAdd = inflate1.findViewById(R.id.tvAdd);
        mRecyclerView = inflate1.findViewById(R.id.recyclerView);
        tvSave0.setOnClickListener(this);
        tvAdd.setOnClickListener(this);
        return inflate1;
    }

    @Override
    public void bindData(List<AcceptCheckInfo.TBean> list) {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
//        for (int i=0;i<2000;i++)
//            list.add(new AcceptCheckInfo.TBean(false,100+i));
        mAdapter = new Item1Adapter(list);
        mRecyclerView.setAdapter(mAdapter);

        ItemDragAndSwipeCallback itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mAdapter);
        itemDragAndSwipeCallback.setSwipeMoveFlags(ItemTouchHelper.START);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
        //设置RecyclerView
        itemTouchHelper.attachToRecyclerView(mRecyclerView);
        // 开启滑动删除
        mAdapter.enableSwipeItem();
        mAdapter.setOnItemSwipeListener(new MyOnItemSwipeListener());
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                final AcceptCheckInfo.TBean bean = mAdapter.getData().get(position);
                TextView tvQty = (TextView) adapter.getViewByPosition(mRecyclerView, position, R.id.tvQty);
                final TextView tvType = (TextView) adapter.getViewByPosition(mRecyclerView, position, R.id.tvType);
                switch (view.getId()) {
                    case R.id.ivAdd:
                        bean.qty += 1;
                        tvQty.setText(Tools.checkedString(bean.qty));
                        break;
                    case R.id.ivReduce:
                        if (bean.qty > 0)
                            bean.qty -= 1;
                        tvQty.setText(Tools.checkedString(bean.qty));
                        break;
                    case R.id.rlChoose:
                        final PopupWindow window = new PopupWindow(mActivity);
                        View inflate = LayoutInflater.from(mActivity).inflate(R.layout.pop_value_choose, null);
                        RecyclerView recyclerView = inflate.findViewById(R.id.recyclerView);
                        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
                        recyclerView.setAdapter(new AcceptStatusAdapter() {
                            @Override
                            protected void onAdapterItemClick(int position) {
                                DictBean dictBean = AcceptCheck.mAcceptStatus.get(position);
                                tvType.setText(Tools.checkedString(dictBean.name));
                                tvType.setTextColor(mActivity.getResources().getColor(R.color.black));
                                bean.conditionId = dictBean.id;
                                bean.remark = dictBean.name;
                                window.dismiss();
                            }
                        });
                        window.setContentView(inflate);
                        window.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                        window.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                        window.setBackgroundDrawable(new ColorDrawable());
                        window.setOutsideTouchable(true);
                        window.setFocusable(true);
                        window.showAsDropDown(tvType, 0, Density.dip2px(mActivity, 20f));
                        break;
                }
            }
        });
    }

    @Override
    protected void isNotFastClick(View v) {
        switch (v.getId()) {
            case R.id.tvAdd:
                int seq = 0;
                List<AcceptCheckInfo.TBean> data = mAdapter.getData();
                for (AcceptCheckInfo.TBean bean : data) {
                    if (bean.seq > seq)
                        seq = bean.seq;
                }
                AcceptCheckInfo.TBean tBean = new AcceptCheckInfo.TBean(true, seq + 1);
                data.add(tBean);
                mAdapter.setNewData(data);
                mRecyclerView.scrollToPosition(data.size() - 1);
                break;
            case R.id.tvSave:
                final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
                loadingDialog.show();
                String jsonData = JsonPostUtil.buildAcceptJson(mAdapter.getData(), mActivity.getBillId(),2);
                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData);
                StaticPresenter.saveReceiveRecordSheet(requestBody, new CallBack<List<AcceptCheckInfo.TBean>>() {
                    @Override
                    public void onCompleted(List<AcceptCheckInfo.TBean> list) {
                        loadingDialog.dismiss();
                        mAdapter.setNewData(list);
                        ToastUtil.showCenterToast("保存成功！");
                    }

                    @Override
                    public void onError() {
                        loadingDialog.dismiss();
                    }
                });
                break;

        }
    }


}
